package com.upcode.weighing.util.sms;
import com.alibaba.fastjson.JSON;
import com.google.gson.JsonObject;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 短信发送
 * @author maoyuhong
 * */
public class SmsApi {

    private static final Logger logger = LoggerFactory.getLogger(SmsApi.class);
    //验证码短信
    public static int sendCode(Map<String,Object> params) throws Exception {
        HttpClient httpClient = new HttpClient();
        //PostMethod postMethod = new PostMethod("http://api.1cloudsp.com/api/v2/send");
        //下面链接需要认证，暂不使用
        PostMethod postMethod = new PostMethod("http://api.1cloudsp.com/api/v2/single_send");
        postMethod.getParams().setContentCharset("UTF-8");
        postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());
        //String co = "您的登录验证码为"+params.get("verCode")+"。工作人员不会向您索要验证码，向您索要验证码的都是骗子。";
        String co = params.get("verCode").toString();
        NameValuePair[] data = {
                new NameValuePair("accesskey", params.get("accesskey").toString()),
                new NameValuePair("secret", params.get("accessSecret").toString()),
                new NameValuePair("sign", params.get("sign").toString()),
                //验证码接口正常时取消注释
                new NameValuePair("templateId", params.get("template").toString()),
                new NameValuePair("mobile", params.get("phone").toString()),
                new NameValuePair("content", URLEncoder.encode(co, "utf-8"))
        };
        postMethod.setRequestBody(data);
        postMethod.setRequestHeader("Connection", "close");

        int statusCode = httpClient.executeMethod(postMethod);
        logger.info("statusCode: " + statusCode + ", body: "
                + postMethod.getResponseBodyAsString());
        Map maps = (Map) JSON.parse(postMethod.getResponseBodyAsString().toString());
        int codeResult = Integer.parseInt(maps.get("code").toString());
        return codeResult;
    }

    //群发短信
    public static int sendMass(Map<String,Object> params) throws Exception {
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod("http://api.1cloudsp.com/api/v2/send");
        postMethod.getParams().setContentCharset("UTF-8");
        postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());

        NameValuePair[] data = {
                new NameValuePair("accesskey", params.get("accesskey").toString()),
                new NameValuePair("secret", params.get("accessSecret").toString()),
                new NameValuePair("sign", params.get("sign").toString()),
                new NameValuePair("data", URLEncoder.encode(params.get("jsonData").toString(), "utf-8"))
        };
        postMethod.setRequestBody(data);
        postMethod.setRequestHeader("Connection", "close");

        int statusCode = httpClient.executeMethod(postMethod);
        logger.info("statusCode: " + statusCode + ", body: "
                + postMethod.getResponseBodyAsString());
        Map maps = (Map) JSON.parse(postMethod.getResponseBodyAsString().toString());
        int codeResult = Integer.parseInt(maps.get("code").toString());
        return codeResult;
    }

    /**
     * 语音下载链接
     * @param params
     * @return
     * @throws Exception
     */
    public static Map getRecordFile(Map<String,Object> params) throws Exception{
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod("http://getRecordFileUrl.do");
        postMethod.getParams().setContentCharset("UTF-8");
        postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());

        NameValuePair[] data = {
                new NameValuePair("num", params.get("num").toString()),
                new NameValuePair("passwd", params.get("passwd").toString()),
                new NameValuePair("lsh", params.get("lsh").toString()),
                new NameValuePair("date", params.get("date").toString())
        };
        postMethod.setRequestBody(data);
        postMethod.setRequestHeader("Connection", "close");
        httpClient.executeMethod(postMethod);
        Map maps = (Map) JSON.parse(postMethod.getResponseBodyAsString().toString());
        return maps;
    }

    public static void main(String[] args) throws Exception {
        /*JsonObject jsonObj = new JsonObject();
        jsonObj.addProperty("15346732107","借鉴借鉴");
        jsonObj.addProperty("13379515921","测试");
        sendMass(jsonObj);

        //JSONObject json = new JSONObject();

//        int a = sendCode("15346732107","123456");*/
//        Map<String,Object> smsParams = new HashMap<>();
//        smsParams.put("accesskey","OmMtzsmyZ4wPTRYz");
//        smsParams.put("accessSecret","AqyYRLdWsudtw2FZugGlSXgzRhLHCSFo");
//        smsParams.put("sign","246001");
//        smsParams.put("template","218819");
//        smsParams.put("phone","15346732107");
//        smsParams.put("verCode","12345");
//        sendCode(smsParams);


        //TextSmsApi t = new TextSmsApi();


        System.out.println("555");
    }
}
